package view.dialog;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.table.AbstractTableModel;

import net.miginfocom.swing.MigLayout;

import java.util.*;

import model.*;

/**
 * Dialog that opens when user clicks on 
 * a room when in floor panel mode.
 *	
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class RoomInfoDialog extends JDialog
{
	private String roomName;
	private String roomType;
	private String occupied;
	private String cleaned;
	private ArrayList<Guests> guestList;

	private JLabel roomNameLabel;
	private JLabel roomTypeLabel;
	private JLabel cleanedTypeLabel;
	private JLabel occupiedTxtLabel;
	private JLabel guestLabel;
	
	private JTable guestTable;
	private JScrollPane pane;
	
	private JButton okayButton;
	private JButton viewGuestInfoButton;
	private JButton surveillanceButton;
	private JPanel buttonPanel;
	
	private MigLayout layout;
	/**
	 * Construct dialog.
	 * @param roomName
	 * @param roomType
	 * @param occupied
	 * @param cleaned
	 * @param list
	 */
	public RoomInfoDialog(String roomName, String roomType, String occupied,
			String cleaned, ArrayList<Guests> list) 
	{
		this.roomName = roomName;
		this.roomType = roomType;
		this.occupied = occupied;
		this.cleaned = cleaned;
		this.guestList = list;
		
		layout = new MigLayout();
		this.setLayout(layout);
		
		// Initialize dialog.
		this.init();
	}
	
	/**
	 * Initialize dialog.
	 */
	public void init()
	{
		this.setTitle("Room Information");
		
		// Initialize labels.
		roomNameLabel = new JLabel(roomName);
		roomTypeLabel = new JLabel("Room Type: " + this.roomType);
		cleanedTypeLabel = new JLabel("Cleaned: " + this.cleaned);
		occupiedTxtLabel = new JLabel("Occupied: " + this.occupied);
		guestLabel = new JLabel("Guest Information:");
		
		// Initialize table.
		guestTable = new JTable(new CustomTableModel());
		
		// Set view port for table.
		guestTable.setPreferredScrollableViewportSize(new Dimension(400, 50));
		
		guestTable.setFillsViewportHeight(true);
		
		// Allow table to sort.
		guestTable.setAutoCreateRowSorter(true);
		
		// Set as single row selection.
		guestTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		
		// Initialize scroll pane.
		pane = new JScrollPane(guestTable);
		
		// Initialize buttons.
		okayButton = new JButton(new ImageIcon("images/check.png"));
		
		viewGuestInfoButton = new JButton(new ImageIcon("images/guest.png"));
		viewGuestInfoButton.setToolTipText("View Guest Profile");
		
		surveillanceButton = new JButton(new ImageIcon("images/camera.png"));
		surveillanceButton.setToolTipText("Room Surveillance");
		
		buttonPanel = new JPanel();
		buttonPanel.add(okayButton);
		buttonPanel.add(viewGuestInfoButton);
		buttonPanel.add(surveillanceButton);
		
		// Add components to dialog.
		this.add(roomNameLabel, "span, wrap");
		this.add(roomTypeLabel, "split 3");
		this.add(cleanedTypeLabel, "gapleft 50");
		this.add(occupiedTxtLabel, "span, gapleft 50, wrap");
		this.add(guestLabel, "span, wrap");
		this.add(pane, "span, wrap");
		this.add(buttonPanel, "span, center");
		
		this.pack();
		this.setResizable(false);
		this.setModal(true);
		
		okayButton.addActionListener(new OkayButtonListener());
		viewGuestInfoButton.addActionListener(new ViewGuestInfoListener());
		surveillanceButton.addActionListener(new SurvCameraListener());
	}
	
	/**
	 * Display dialog.
	 */
	public void open()
	{
		this.setVisible(true);
	}
	
	/**
	 * Method used to set columns of the guest table.
	 * @return
	 */
	public ArrayList<String> setColumns()
	{
		// Create array list.
		ArrayList<String> columns = new ArrayList<String>();
		
		// Add column name.
		columns.add("Name");
		columns.add("ID");
		columns.add("Check-In Date");
		columns.add("Check-Out Date");
		
		return columns;
	}
	
	/*
	 * Custom table model. 
	 * @author Kenny
	 */
	class CustomTableModel extends AbstractTableModel
	{
		// Set columns.
		final ArrayList<String> columns = setColumns();
		
		public String getColumnName(int col)
		{
			return columns.get(col);
		}
		
		@Override
		public int getColumnCount() {
			return columns.size();
		}

		@Override
		public int getRowCount() {
			return guestList.size();
		}

		@Override
		public Object getValueAt(int row, int column) 
		{
			switch(column)
			{
			case 0:
				return guestList.get(row).getName();
			case 1:
				return guestList.get(row).getID();
			case 2: 
				return guestList.get(row).getAdmitDate();
			case 3:
				return guestList.get(row).getLeaveDate();
			default:
				return "";
			}
		}
	}
	
	/*
	 * Listener for okay button. Closes the dialog.
	 * @author Kenny
	 */
	private class OkayButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{
			RoomInfoDialog.this.setVisible(false);	
		}
	}
	
	/*
	 * Listener for view guest info button. Opens up a new dialog.
	 * @author Kenny
	 */
	private class ViewGuestInfoListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			// Retrieve selected row from table.
			int row = RoomInfoDialog.this.guestTable.getSelectedRow();
			
			// User has not selected a patient.
			if (row == -1)
			{
				JOptionPane.showMessageDialog(RoomInfoDialog.this,
						"Please select a guest from list!",
						"No Guest Selected", JOptionPane.ERROR_MESSAGE);
				return;
			} else {
			// Retrieve guest ID.
			int ID = (Integer) guestTable.getValueAt(row, 1);
			
			Guests guest = null;
			
			// Retrieve the guest by searching.
			for (Guests g : guestList)
			{
				if (g.getID() == ID)
				{
					guest = g;
				}
			}
			
			if (guest != null)
			{
				String gName = guest.getName();
				String gID = "" + guest.getID();
				String gPhone = guest.getPhoneNum();
				String roomName = guest.getCurrentRoom().getRoomName();
				String checkInTime = "" + guest.getAdmitDate();
				String checkOutTime = "" + guest.getLeaveDate();
				
				BufferedImage profileImage = guest.getProfileImg();
				
				GuestInfoDialog dialog = new GuestInfoDialog(gName, gID,
							roomName, checkInTime, checkOutTime, gPhone,
							profileImage);
				
				// Open dialog.
				dialog.open();
			}
			
			}
		}
	}
	
	/*
	 * Listener for surveillance camera. Does nothing at the moment. 
	 */
	private class SurvCameraListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			JOptionPane.showMessageDialog(RoomInfoDialog.this,
					"No, this doesn't work you pervert.",
					"Surveillance Disabled", JOptionPane.ERROR_MESSAGE);
		}
	}
}
